
package jmind.core.test;

import com.alibaba.fastjson.JSON;
import jmind.base.util.RandUtil;
import jmind.base.util.XMLUtils;

import javax.xml.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: xieweibo
 * @Date: 2018/6/7
 *
 * package-info.java 定义了XmlSchema
 */

@XmlRootElement(name = "productList")
@XmlAccessorType(XmlAccessType.FIELD)
public class ProductList {

    @XmlElement(name="product")
    private List<Product> products=new ArrayList<>();

    public List<Product> getProducts() {
        return products;
    }

    public void setProducts(List<Product> products) {
        this.products = products;
    }

    public void add(Product product){
        this.products.add(product);
    }


    public static void main(String[] args) {
        ProductList productList=new ProductList();
        for (int i=0;i<10;i++){
            Product product=new Product();
            product.setProductCode("p"+i);
            product.setProductName(i+"abc");
            SubType subType=new SubType();
            subType.setAuthorizedNo(i+"");
            subType.setTypeNo(RandUtil.randomCode(4));
            List<ProdCodes> p=new ArrayList<>();
            for(int j=10;j<13;j++){
                ProdCodes prodCodes=new ProdCodes();
                prodCodes.setCode(RandUtil.randomCode(5));
                prodCodes.setCodeLevel("1");
                prodCodes.setCodeVersion("2");
                p.add(prodCodes);
            }
            subType.setResProdCodes(p);

            product.setSubType(subType);
            productList.add(product);
        }
        String s = XMLUtils.toXML(productList, false);
        System.out.println(s);
        ProductList productList1 = XMLUtils.fromXML(ProductList.class, s);
        System.err.println(JSON.toJSONString(productList1));
    }
}
